<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div style="display: inline-block;margin: 0px 2px;">
        <el-button
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-plus"
          @click="add"
        >新增</el-button>
      </div>
      <el-button
        type="danger"
        class="filter-item"
        size="mini"
        icon="el-icon-refresh"
        @click="refresh"
      >刷新</el-button>
      <!--表单组件-->
      <aForm ref="form" :is-add="isAdd" />
      <eForm ref="formE" :is-add="isEdit" />



      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column  prop="id" label="id" />
        <!--
          <el-table-column  prop="image" label="活动封面">
            <template slot-scope="scope">
              <a :href="scope.row.image" style="color: #42b983" target="_blank">
                <img :src="scope.row.image" alt="点击打开" class="table-img" />
              </a>
            </template>
          </el-table-column>
        -->
          
        <el-table-column  prop="title" label="活动名称" />


          <!--
            <el-table-column  prop="prizeMsg" label="活动介绍" />
            <el-table-column  prop="prizeRule" label="活动规则" />
          -->

        
        <el-table-column  prop="startTime" label="开始时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column  prop="endTime" label="结束时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column  prop="status" label="活动状态">
          <template slot-scope="scope">
            <div>
              <el-tag v-if="scope.row.status === 0" style="cursor: pointer" :type="'info'">已到期</el-tag>
              <el-tag v-if="scope.row.status === 1" style="cursor: pointer" :type="''">进行中</el-tag>
              <el-tag v-if="scope.row.status === 2" style="cursor: pointer" :type="'info'">未开启</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column  prop="isLook" label="活动状态">
            <template slot-scope="scope">
              <div @click="onLook(scope.row.id,scope.row.isLook)">
                <el-tag v-if="scope.row.isLook === 0" style="cursor: pointer" :type="''">启用</el-tag>
                <el-tag v-if="scope.row.isLook === 1" style="cursor: pointer" :type="'info'">禁用</el-tag>
              </div>
          </template>
        </el-table-column>
        <el-table-column prop="number" label="每日抽奖次数" />
        <el-table-column prop="integral" label="单次消耗积分" />
        <el-table-column prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        
        <el-table-column v-permission="['admin','yxSystemPrize:edit','yxSystemPrize:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">

            <!--
              <udOperation
                :data="scope.row"
                :permission="permission"
              />
            -->
            <el-button
                      size="mini"
                      type="primary"
                      @click="editDetail(scope.row)"
            >
            编辑</el-button>
            <el-popover
                   size="mini"
                  :ref="scope.row.id"
                  v-permission="['admin','YXSTORECOUPON_ALL','YXSTORECOUPON_DELETE']"
                  placement="top"
                 
                >
                  <p>确定删除本条数据吗？</p>
                  <div style="text-align: right; margin: 0">
                    <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
                    <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
                  </div>
                  <el-button slot="reference" type="danger" size="mini">删除</el-button>
            </el-popover>
            
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import aForm from './form'
import eForm from './formE'
import cgood from '@/views/components/good'
import {crudYxSystemPrize,onlook,del} from '@/api/yxSystemPrize'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";

// crud交由presenter持有
const defaultCrud = CRUD({ title: '', url: 'api/yxSystemPrize', sort: 'id,desc', crudMethod: { ...crudYxSystemPrize }})
const defaultForm = { id: null, image: null, title: null, prizeMsg: null, prizeRule: null, startTime: null, endTime: null, status: null, isLook: null, number: null, integral: null, createTime: null, updateTime: null }
export default {
  name: 'YxSystemPrize',
  components: { pagination, crudOperation, rrOperation, udOperation ,MaterialList,cgood,aForm,eForm},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['is_enable'],
  data() {
    return {
      delLoading: false,
      isAdd:false,
      isEdit:false,
      form1: {
        good:{
          productId: null,
          storeName: null,
          image: null,
          otPrice: null,
          price: null,
        }
      },

      imageArr:[],
      permission: {
        add: ['admin', 'yxSystemPrize:add'],
        edit: ['admin', 'yxSystemPrize:edit'],
        del: ['admin', 'yxSystemPrize:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'title', display_name: '活动名称' }
      ]
    }
  },
  watch: {
    "imageArr":function(val) {
      if (val) {
        this.form.image = val.join(",");
      }
    },
  },
  methods: {

    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.title
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.image && form.id) {
        this.imageArr = form.image.split(",");
      }else{
        this.imageArr=[]
        console.log(111111)
      }
    },


    subDelete(id) {
      this.delLoading = true
      let ids = [id]
      del(ids).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.crud.toQuery();
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
      })
    },
    refresh(){
      this.crud.toQuery();
    },

    editDetail(data) {
      this.isEdit = false
      const _this = this.$refs.formE
      _this.formE = {
        id:data.id,
        image:data.image,
        title:data.title,
        prizeMsg:data.prizeMsg,
        prizeRule:data.prizeRule,
        startTime:data.startTime,
        endTime:data.endTime,
        number:data.number,
        integral:data.integral,
        tableData:data.tableData,
        imageArrE:[data.image]
      }
      _this.dialog = true
    },
    add() {
      this.isAdd = true
      this.$refs.form.dialog = true
    },
    onLook(id, status) {
      this.$confirm(`确定进行[${status ? '启用' : '禁用'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          onlook(id, { status: status }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.refresh()
              }
            })
          })
        })
        .catch(() => { })
    }
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>
